

//根据天气URL返回天气数据


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.kim.rssreader.activity.StarterActivity;

import android.os.Message;

public class WeatherUtil {
	private static String CONNET_ERRIR = "请求错误";
	final String url = "http://m.weather.com.cn/data/101280601.html";//天气API
	
	public static String connet(String baseurl) {
		String result = "";
		try {
			URL url = new URL( baseurl);
			try {
				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();
				if (conn.getResponseCode() != 200) {// 从Internet获取网页,发送请求,将网页以流的形式读回来
					return CONNET_ERRIR;
				}else{
					InputStream is = conn.getInputStream();
					try {
						result =InputStreamTOString(is);
						System.out.println("result=" + result);
					} catch (Exception e) {
						e.printStackTrace();
					} // 文件流输入出文件用outStream.write
				}
					
				
				conn.disconnect();

			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	//根据url返回天气数据
//	public static void connet001(String baseurl) {
//		String result = "";
//		Message msg = new Message();
//		try {
//			URL url = new URL( baseurl);
//			try {
//				HttpURLConnection conn = (HttpURLConnection) url
//						.openConnection();
//				if (conn.getResponseCode() != 200) {// 从Internet获取网页,发送请求,将网页以流的形式读回来
//					msg.obj =  CONNET_ERRIR;
//					StarterActivity.myhandler.sendMessage(msg);
//				}else{
//					InputStream is = conn.getInputStream();
//					try {
//						result =InputStreamTOString(is);
//						System.out.println("result=" + result);
//						msg.obj =  result;
//						StarterActivity.myhandler.sendMessage(msg);
//					} catch (Exception e) {
//						e.printStackTrace();
//					} // 文件流输入出文件用outStream.write
//				}
//					
//				
//				conn.disconnect();
//
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		} catch (MalformedURLException e) {
//			e.printStackTrace();
//		}
//	}
	
	final static int BUFFER_SIZE = 4096;
	
	public static String InputStreamTOString(InputStream in) throws Exception {

		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] data = new byte[BUFFER_SIZE];
		int count = -1;
		while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
			outStream.write(data, 0, count);

		data = null;
		return new String(outStream.toByteArray(), "UTF-8");
	}

}
